﻿using CapWeb251.Controllers;
using CapWeb251.Services;
using DotnetFramework.CAP.Serilog;
using DotNetFramework.CAP;
using DotNetFramework.CAP.Core.Logger;
//using DotnetFramework.CAP.Serilog;
//using DotNetFramework.CAP;
//using DotNetFramework.CAP.Core.Ioc;
//using DotNetFramework.CAP.Core.Logger;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;

namespace CapWeb251
{
    public class CapConfig
    {
        public static DotNetFramework.CAP.Core.Ioc.IServiceCollection Services { get; set; }

        public static void RegisterCap()
        {
            Services = new DotnetFramework.CAP.Ioc.ServiceCollection();
            Services.AddCap(stetup =>
            {
                // 注册节点到 Consul
                //stetup.UseSqlServer("Data Source=localhost;database=EFCodeFirst;Uid=sa;pwd=sa;");
                stetup.UseMySql("Server=192.168.231.132;port=3306;database=CapDemo;user=root;password=123456;Pooling=True;Max Pool Size=500;Min Pool Size=5;");
                stetup.UseRabbitMQ(option =>
                {
                    //option.VirtualHost = "192.168.231.132";
                    option.HostName = "192.168.231.132";
                    option.Port = 5672;
                    option.UserName = "admin";
                    option.Password = "admin";
                });
                stetup.DefaultGroup = "cap.queue.capweb251.v1 ";
            }, services =>
            {
                services.AddSingleton<DotNetFramework.CAP.NodeDiscovery.IDiscoveryProviderFactory, DotnetFramework.CAP.Consul.DiscoveryProviderFactory>();
            }, services =>
            {
                //services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
                services.TryAddSingleton<ILogger, Logger>();
                //services.TryAddSingleton<ILoggerFactory, LoggerFactory>();
            });

            Services.AddScopedMuti<ICapSubscribe, OpService>();
            Services.AddScopedMuti<ICapSubscribe, Op2Service>();
            Services.AddScopedMuti<ICapSubscribe, Op2MysqlService>();

            Services.BeginRegister();
            Services.ServiceProvider.GetService<IBootstrapper>().BootstrapAsync(new CancellationToken());
        }
    }
}